﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Mani.Common;
using Ghasam.Business.Services;
using Ghasam.Business.Domain.Entity;

public partial class NewCheque : System.Web.UI.Page {
    protected void Page_Load( object sender, EventArgs e ) {
        if (!Page.IsPostBack)
            txtChequeReceiveDate.Text = MyPersianDate.DateTimeToPersian(DateTime.Now);
        fillCustomerList();
    }
    private IAccountingService accountingService;
    private IPaymentService paymentService;
    private ICustomerService customerService;
    private int _isFormal;
    private int isFromal
    { get { if (_isFormal != null) return _isFormal; } }

    public IPaymentService PaymentService
    {
        get { return paymentService; }
        set { paymentService = value; }
    }
    public IAccountingService AccountingService
    {
        set { accountingService = value; }
        get { return accountingService; }
    }
    public ICustomerService CustomerService
    {
        set { customerService = value; }
        get { return customerService; }
    }
    private void fillCustomerList()
    {
        Customer customer = new Customer();
        checkFormalCustomer();
        customer.IsFormal = Convert.ToBoolean(_isFormal);
        ddlCustomers.DataSource = customerService.GetCustomers(customer, 0, 2000);
        ddlCustomers.DataBind();
    }
    protected void checkFormalCustomer(){
        bool show = CommonHelper.ShowHiddenCustomers();
        _isFormal = 0;

        if (show) {
            _isFormal = -1;
        }
    }

    protected void btnSave_Click( object sender, EventArgs e ) {
        DateTime chequeDate = MyPersianDate.PersianToDateTime( txtChequeDate.Text );
        DateTime receivedDate = MyPersianDate.PersianToDateTime(txtChequeReceiveDate.Text);
        int cashed = (cbCashed.Checked ? 1 : 0);
        string customerid  = ddlCustomers.SelectedValue;
        int amount = Convert.ToInt32(txtAmount.Text);
        checkFormalCustomer();

        PaymentService.PaymentSaveOrUpadate(Convert.ToInt32(customerid) ,0,amount,txtChequeNumber.Text,txtBankName.Text,
            txtBranchName.Text, chequeDate, txtComment.Text, Convert.ToBoolean(cashed), null, DateTime.Now.Date, receivedDate, DateTime.Now.Date);

        AccountingService.AccountingSaveOrUpadate(Convert.ToInt32(customerid),Convert.ToBoolean( _isFormal), 0,amount, "چک شماره " + txtChequeNumber.Text, DateTime.Now.Date, DateTime.Now.Date);

        Response.Redirect( "ChequeList.aspx" );
    }
}